#pragma once
#include "../SharedLibs/CygiaSocket/include/CygiaSocketLib.h"


typedef std::function<void(char *pData, unsigned int nDataSize)> ONREADLISTENER;

class CSocketClient
{
public:
	CSocketClient();
	~CSocketClient();


public:
	bool IsConnected();
	std::string & getAddress();
	void setAddress(std::string address);
	int getServerPort();
	void setServerPort(int port);
	int getLocalPort();
	void setLocalPort(int port);
	std::string& getLocalAddr();
	int send(const char *pszBuffer, int nLen);
	void delayConnect();
	void setOnReadListener(ONREADLISTENER listener);

private:
	inline int initSocket();
	int termSocket();
	static UINT ReconnectThreadFunction(LPVOID lpvData);
	UINT ReconnectThreadFunctionInner();


private:
	HCHANNEL m_hClientChannel;
	std::string m_strServerAddress;
	unsigned int m_nServerPort;
	std::string m_strLocalAddr;
	unsigned int m_nLocalPort;
	HANDLE m_hEventReconnect;
	HANDLE m_hEventThreadExit;
	HANDLE m_hEventReconnectResult;
	int m_nConnectedResultCode;
	BOOL m_bConnecting;
	BOOL m_bAutoReConnect;
	ONREADLISTENER m_funOnReadListener;
};

